import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class RequestService {

  constructor() { }
  getPromise() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const num: number = Math.ceil(Math.random() * 20); // 生成1-10的随机数
        console.log(num);
        if (num <= 10) {
          resolve(num);
        } else {
          reject(num + '大于10');
        }
      }, 2000);
    });
  }
  getRx() {
    let stream = new Observable((observer) => {
      setTimeout(() => {
        observer.next('rxjs');
      }, 3000);
    });
    return stream;
  }
  getRxs() {
    let stream = new Observable((observer) => {
      let count:number = 0
      setInterval(() => {
        observer.next(count++);
      }, 1000);
    });
    return stream;
  }
}
